package com.example.armmvvm.ex

import com.example.arm.util.ArmsUtils
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Consumer
import io.reactivex.schedulers.Schedulers

fun Disposable.addTo(c: CompositeDisposable): Disposable {
    c.add(this)
    return this
}

fun <T> Observable<T>.subscribeWithLog(onNext: Consumer<T>): Disposable {
    return this.subscribe(onNext, Throwable::printStackTrace)
}

fun <T> Observable<T>.subscribeWithToast(onNext: Consumer<T>): Disposable {
    return this.subscribe(onNext) {
        it.printStackTrace()
        ArmsUtils.makeText(it.message)
    }
}

fun <T> Observable<T>.switchMain(): Observable<T> {
    return this.subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
}